/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EmployeesDialog.java
 *
 * Created on Mar 19, 2010, 4:44:03 PM
 */

package org.foment.gem.dialogs;

import java.awt.*;
import java.sql.*;
import javax.swing.*;
import org.foment.data.SqlDataResultSet;
import org.foment.gem.beans.EmployeeInsuranceEditor;
import org.foment.gem.beans.EmployeeWorkStagnationPanel;
import org.foment.joliage3.reports.Report;
import org.foment.joliage3.sql.Database;

import static org.foment.joliage3.core.internationalization.Internationalization.i18n;

/**
 *
 * @author Dusan
 */
public class EmployeesDialog extends GemInternalDialog {




    /** Creates new form EmployeesDialog */
    public EmployeesDialog() {
        initComponents();

        empoyeeCoursesEditor = new org.foment.gem.beans.EmployeeCoursesEditor();
        pnlPrint.add(empoyeeCoursesEditor, java.awt.BorderLayout.CENTER);
        
        employeePositionsEditor = new org.foment.gem.beans.EmployeePositionsEditor();
        pnlPositions.add(employeePositionsEditor, java.awt.BorderLayout.CENTER);

        employeesBrowser = new org.foment.gem.beans.EmployeesBrowser();
        employeesBrowser.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                employeesBrowserPropertyChange(evt);
            }
        });

        employeeEducationEditor = new org.foment.gem.beans.EmployeeEducationEditor();
        pnlEducation.add(employeeEducationEditor, java.awt.BorderLayout.CENTER);
        employeeDataEditor = new org.foment.gem.beans.EmployeeDataEditor();

        org.jdesktop.layout.GroupLayout pnlEmployeeDataEditorLayout = new org.jdesktop.layout.GroupLayout(pnlEmployeeDataEditor);
        pnlEmployeeDataEditor.setLayout(pnlEmployeeDataEditorLayout);
        pnlEmployeeDataEditorLayout.setHorizontalGroup(
            pnlEmployeeDataEditorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, employeeDataEditor, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 591, Short.MAX_VALUE)
        );
        pnlEmployeeDataEditorLayout.setVerticalGroup(
            pnlEmployeeDataEditorLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, employeeDataEditor, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 444, Short.MAX_VALUE)
        );

        // kreiramo instancu svakog editora i dodajemo na panel.
        employeeInsuranceEditor = new  EmployeeInsuranceEditor();
        pnlInsurance.add(employeeInsuranceEditor, java.awt.BorderLayout.CENTER);
        
        btnPositions.setVisible(false);
        btnPrint.setVisible(false);
        btnInsurance.setVisible(false);
        btnEducation.setVisible(false);

        /*
        employeeReports = new org.foment.gem.beans.EmployeeReports();
        pnlPrint.add(employeeReports);
         */
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlTop = new javax.swing.JPanel();
        btnBasicData = new javax.swing.JLabel();
        btnPositions = new javax.swing.JLabel();
        btnPrint = new javax.swing.JLabel();
        btnInsurance = new javax.swing.JLabel();
        btnEducation = new javax.swing.JLabel();
        pnlMain = new javax.swing.JPanel();
        pnlEmployeeDataEditor = new javax.swing.JPanel();
        pnlPositions = new javax.swing.JPanel();
        pnlPrint = new javax.swing.JPanel();
        pnlInsurance = new javax.swing.JPanel();
        pnlEducation = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        btnBasicData.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/foment/gem/images/employees_basic_data.png"))); // NOI18N
        btnBasicData.setText(i18n("<html><b>Basic data</b><br>Basic data about employees</html>"));
        btnBasicData.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnBasicDataMouseClicked(evt);
            }
        });

        btnPositions.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/foment/gem/images/employees_jobs.png"))); // NOI18N
        btnPositions.setText(i18n("<html><b>Positions</b><br>Employee positions (jobs)</html>"));
        btnPositions.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnPositionsMouseClicked(evt);
            }
        });

        btnPrint.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/foment/gem/images/employees_print.png"))); // NOI18N
        btnPrint.setText(i18n("<html>Podaci o predmetima</html>"));
        btnPrint.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnPrintMouseClicked(evt);
            }
        });

        btnInsurance.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/foment/gem/images/dialog_LBO_icon.png"))); // NOI18N
        btnInsurance.setText("<html>Podaci o<br>osiguranicima</html>");
        btnInsurance.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnInsuranceMouseClicked(evt);
            }
        });

        btnEducation.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/foment/gem/images/dialog_Education_icon.png"))); // NOI18N
        btnEducation.setText("Obrazovanje");
        btnEducation.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnEducationMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout pnlTopLayout = new javax.swing.GroupLayout(pnlTop);
        pnlTop.setLayout(pnlTopLayout);
        pnlTopLayout.setHorizontalGroup(
            pnlTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlTopLayout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(btnBasicData)
                .addGap(31, 31, 31)
                .addComponent(btnPositions)
                .addGap(30, 30, 30)
                .addComponent(btnPrint)
                .addGap(37, 37, 37)
                .addComponent(btnInsurance)
                .addGap(40, 40, 40)
                .addComponent(btnEducation)
                .addGap(23, 23, 23))
        );
        pnlTopLayout.setVerticalGroup(
            pnlTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlTopLayout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addGroup(pnlTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnBasicData)
                        .addComponent(btnPositions)
                        .addComponent(btnPrint))
                    .addGroup(pnlTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnEducation)
                        .addComponent(btnInsurance))))
        );

        getContentPane().add(pnlTop, java.awt.BorderLayout.PAGE_START);

        pnlMain.setLayout(new java.awt.CardLayout());

        javax.swing.GroupLayout pnlEmployeeDataEditorLayout = new javax.swing.GroupLayout(pnlEmployeeDataEditor);
        pnlEmployeeDataEditor.setLayout(pnlEmployeeDataEditorLayout);
        pnlEmployeeDataEditorLayout.setHorizontalGroup(
            pnlEmployeeDataEditorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 759, Short.MAX_VALUE)
        );
        pnlEmployeeDataEditorLayout.setVerticalGroup(
            pnlEmployeeDataEditorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 347, Short.MAX_VALUE)
        );

        pnlMain.add(pnlEmployeeDataEditor, "cardBasicData");

        pnlPositions.setLayout(new java.awt.BorderLayout());
        pnlMain.add(pnlPositions, "cardPositions");

        pnlPrint.setLayout(new java.awt.BorderLayout());
        pnlMain.add(pnlPrint, "cardPrint");

        pnlInsurance.setLayout(new java.awt.BorderLayout());
        pnlMain.add(pnlInsurance, "cardInsurance");

        pnlEducation.setLayout(new java.awt.BorderLayout());
        pnlMain.add(pnlEducation, "cardEducation");

        getContentPane().add(pnlMain, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBasicDataMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnBasicDataMouseClicked
        // TODO add your handling code here:
        showData();
    }//GEN-LAST:event_btnBasicDataMouseClicked

    private void btnPositionsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnPositionsMouseClicked
        // TODO add your handling code here:
        showPositions();
    }//GEN-LAST:event_btnPositionsMouseClicked

    private void btnPrintMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnPrintMouseClicked
        // TODO add your handling code here:
        showPrinting();
    }//GEN-LAST:event_btnPrintMouseClicked

    private void btnInsuranceMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnInsuranceMouseClicked
        // TODO add your handling code here:
        showInsurance();
    }//GEN-LAST:event_btnInsuranceMouseClicked

    private void btnEducationMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnEducationMouseClicked
        // TODO add your handling code here:
        showEducation();
    }//GEN-LAST:event_btnEducationMouseClicked

public void showData() {
    showCard(btnBasicData, "cardBasicData");
  }

  public void showPositions() {
    showCard(btnPositions, "cardPositions");
  }

  public void showPrinting() {
    showCard(btnPrint, "cardPrint");
  }
  public void showInsurance()
  {
      showCard(btnInsurance, "cardInsurance");
  }

  public void showEducation()
  {
      showCard(btnEducation, "cardEducation");
  }

  private void showCard(javax.swing.JLabel label, String cardName) {
    java.awt.CardLayout layout = (java.awt.CardLayout)pnlMain.getLayout();
    layout.show(pnlMain, cardName);

    btnBasicData.setOpaque(false);
    btnPositions.setOpaque(false);
    btnPrint.setOpaque(false);
    btnInsurance.setOpaque(false);
    btnEducation.setOpaque(false);
    
    pnlTop.repaint();

    label.setOpaque(true);

  }


  public void setActiveEmployee(int ID) {
    if (ID == -1) {
        btnPositions.setVisible(false);
        btnPrint.setVisible(false);
        btnInsurance.setVisible(false);
        btnEducation.setVisible(false);
        employeeDataEditor.setEditing(false);

        return ;
      }
      try {
        ResultSet rs = Database.getTable("Employee").getResultSet();

        Database.getTable("Employee").pushState();

        rs.beforeFirst();
        while (rs.next()) {
          if (rs.getInt("EmployeeID") == ID) {
            if (Database.getTable("Employee").editCurrentRow()) {
              Database.getTable("Employee").removeState();
              employeePositionsEditor.update();
              //employeeReports.update();
              employeeInsuranceEditor.update();
              employeeEducationEditor.update();
              empoyeeCoursesEditor.update();
              // ovaj panel smo dodali naknadno i moramo ga update-ovati posebno
              employeeDataEditor.getEmployeeWorkStagnationPanel().update();
            } else
              Database.getTable("Employee").popState();
            break;
          }
        }

        btnPositions.setVisible(true);
        btnPrint.setVisible(true);
        btnInsurance.setVisible(true);
        btnEducation.setVisible(true);
        employeeDataEditor.setEditing(true);

      } catch (SQLException ex) {
        ex.printStackTrace();
      }
  }


    public Component getNavigatorComponent() {
        return employeesBrowser;
    }

    private void employeesBrowserPropertyChange(java.beans.PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals("selectedEmployeeID")) {
            setActiveEmployee(((Integer)evt.getNewValue()).intValue());
    }
  }



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel btnBasicData;
    private javax.swing.JLabel btnEducation;
    private javax.swing.JLabel btnInsurance;
    private javax.swing.JLabel btnPositions;
    private javax.swing.JLabel btnPrint;
    private javax.swing.JPanel pnlEducation;
    private javax.swing.JPanel pnlEmployeeDataEditor;
    private javax.swing.JPanel pnlInsurance;
    private javax.swing.JPanel pnlMain;
    private javax.swing.JPanel pnlPositions;
    private javax.swing.JPanel pnlPrint;
    private javax.swing.JPanel pnlTop;
    // End of variables declaration//GEN-END:variables

    private org.foment.gem.beans.EmployeeDataEditor employeeDataEditor;
    private org.foment.gem.beans.EmployeePositionsEditor employeePositionsEditor;
    private org.foment.gem.beans.EmployeeCoursesEditor empoyeeCoursesEditor;
    private org.foment.gem.beans.EmployeesBrowser employeesBrowser;
    private org.foment.gem.beans.EmployeeReports employeeReports;
    private org.foment.gem.beans.EmployeeInsuranceEditor employeeInsuranceEditor;
    private org.foment.gem.beans.EmployeeEducationEditor employeeEducationEditor;
}
